<script lang="ts" setup>
import type { BaseinfoProductApi } from '#/api/csyt/baseinfoproduct';

import { computed, ref } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { Tabs } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { getBaseinfoProduct } from '#/api/csyt/baseinfoproduct';

import { useFormSchema } from '../data';
import BaseinfoLableinfoForm from './baseinfo-lableinfo-form.vue';
import BaseinfoProductDetailsForm from './baseinfo-product-details-form.vue';

const emit = defineEmits(['success']);
const formData = ref<BaseinfoProductApi.BaseinfoProduct>();
// const isDeleteButton = ref(false);
const getTitle = computed(() => {
  return '查看产品信息';
});

/** 子表的表单 */
const subTabsName = ref('baseinfoLableinfo');
const baseinfoLableinfoFormRef =
  ref<InstanceType<typeof BaseinfoLableinfoForm>>();
const baseinfoProductDetailsFormRef =
  ref<InstanceType<typeof BaseinfoProductDetailsForm>>();

const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
  submitButtonOptions: {
    show: true,
    label: '完成',
  },
});

const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    modalApi.lock();
    try {
      // 关闭并提示
      await modalApi.close();
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    let data = modalApi.getData<BaseinfoProductApi.BaseinfoProduct>();
    if (!data) {
      return;
    }
    if (data.id) {
      modalApi.lock();
      try {
        data = await getBaseinfoProduct(data.id);
      } finally {
        modalApi.unlock();
      }
    }
    // 设置到 values
    formData.value = data;
    await formApi.setValues(formData.value);
  },
});
</script>

<template>
  <Modal :title="getTitle">
    <Form class="mx-4" />
    <!-- 子表的表单 -->
    <Tabs v-model:active-key="subTabsName">
      <Tabs.TabPane key="baseinfoLableinfo" tab="标签信息" force-render>
        <BaseinfoLableinfoForm
          ref="baseinfoLableinfoFormRef"
          :product-no="formData?.productNo"
        />
      </Tabs.TabPane>
      <Tabs.TabPane
        key="baseinfoProductDetails"
        tab="产品信息详情"
        force-render
      >
        <BaseinfoProductDetailsForm
          ref="baseinfoProductDetailsFormRef"
          :product-no="formData?.productNo"
        />
      </Tabs.TabPane>
    </Tabs>
  </Modal>
</template>
